﻿
local L = {
	chatshowtip = "开启鼠标经过聊天窗口显示装备详情",
	chatshowequip = "开启聊天窗口装备比较",
	chatshift = "聊天窗口按下Shift才比较装备",
	actionshowequip = "开启动作条上装备比较",
	refshowequip = "开启点击出的物品提示的装备比较",
	shift = "按下Shift才比较装备",
	rating = "开启属性转换",
	itemlevel = "显示物品等级",
	itemid = "显示物品ID",
	disable = "|cffaaaaaa(禁用:已使用%s)|r",
}

CURRENTLY_EQUIPPED = "|cffffff9a==="..CURRENTLY_EQUIPPED.."===|r"
local _G = getfenv(0)
local find = string.find

local GameTooltip = GameTooltip
local ItemRefTooltip = ItemRefTooltip
--local GameTooltip1, GameTooltip2, GameTooltip3, GameTooltip4

-- create tooltip func
local function CreateTooltip(name, owner)
	local tip = CreateFrame("GameTooltip", name, owner, "GameTooltipTemplate")
	tip:SetFrameStrata("TOOLTIP")
	tip:SetClampedToScreen(true)
	_G[name.."TextLeft2"]:SetFont(GameTooltipTextLeft1:GetFont())

	if Bagnon_HookTip then
		Bagnon_HookTip(tip, "SetHyperlinkCompareItem")
	end
	if SellFish_HookTip_ByScript then
		SellFish_HookTip_ByScript(tip, "SetHyperlinkCompareItem")
	end

	return tip
end

-- hook frame func
local function HookFrame(frame, script, func)
	if not frame or not script or not func then return end
	if frame:GetScript(script) then
		frame:HookScript(script, func)
	else
		frame:SetScript(script, func)
	end
end

local function UpdateTooltip()
	if not tdCompareDB.chatshowequip then return end
	if IsShiftKeyDown() or not tdCompareDB.chatshift then
		if not GameTooltip1:IsVisible() then
			GameTooltip_ShowCompareItem()
		end
	else
		if GameTooltip1:IsVisible() then
			GameTooltip1:Hide()
			GameTooltip2:Hide()
		end
	end
end

local function OnHyperlinkEnter(frame, link, ...)
	if not tdCompareDB.chatshowtip then return end 
	if find(link, "^item") or find(link, "^enchant") or find(link, "^spell") or find(link, "^quest") or find(link, "^unit") then
		GameTooltip:SetOwner(frame, "ANCHOR_TOPLEFT")
		GameTooltip:SetHyperlink(link)
		GameTooltip:Show()
		UpdateTooltip()
	end
end

local function OnHyperlinkLeave(frame, ...)
	GameTooltip:Hide()
end

-- create tooltip for GameTooltip
GameTooltip1 = CreateTooltip("GameTooltip1", GameTooltip)
GameTooltip2 = CreateTooltip("GameTooltip2", GameTooltip)

-- ItemRefTooltip
GameTooltip3 = CreateTooltip("GameTooltip3", ItemRefTooltip)
GameTooltip4 = CreateTooltip("GameTooltip4", ItemRefTooltip)

HookFrame(GameTooltip, "OnTooltipSetItem", function(frame, ...)
	if tdCompareDB.shift then return end
	if not GameTooltip1:IsVisible() then
		GameTooltip_ShowCompareItem();
	end
end)

HookFrame(ItemRefTooltip, "OnTooltipSetItem", function(frame, ...)
	if not tdCompareDB.refshowequip then return end
	GameTooltip_ShowCompareItem(this, GameTooltip3, GameTooltip4)
end)

-- chat frame
for i = 1, NUM_CHAT_WINDOWS do
	local frame = _G["ChatFrame"..i]

	HookFrame(frame, "OnHyperlinkEnter", OnHyperlinkEnter)
	HookFrame(frame, "OnHyperlinkLeave", OnHyperlinkLeave)
	frame.UpdateTooltip = UpdateTooltip
end

function GameTooltip_ShowCompareItem(tip, tip1, tip2)
	if not tip then tip = GameTooltip end

	local link = select(2, tip:GetItem());
	if not link then return end

	local f = GetMouseFocus() and GetMouseFocus():GetName() or ""
	if f == "WorldFrame" or
	       (_G[f.."HotKey"] and not tdCompareDB.actionshowequip) or
	       find(f, "^Character.*Slot$") or
	       find(f, "^TempEnchant%d+$") then
		return
	end

	if not tip1 then tip1 = GameTooltip1; end
	if not tip2 then tip2 = GameTooltip2; end

	local item1, item2 = tip1:SetHyperlinkCompareItem(link, 1), tip2:SetHyperlinkCompareItem(link, 2);

	if not item1 and not item2 then return; end

	local left, right, anchor1, anchor2 = tip:GetLeft(), tip:GetRight(), "TOPLEFT", "TOPRIGHT"
	if not left or not right then return; end

	if (GetScreenWidth() - right) < left then anchor1, anchor2 = anchor2, anchor1 end
	local changed
	if item2 and not item1 then
		tip1, tip2, item1, item2 = tip2, tip1, true, nil;
		changed = true
	end

	if item1 then
		tip1:SetOwner(tip, "ANCHOR_NONE")
		tip1:ClearAllPoints()
		tip1:SetPoint(anchor1, tip, anchor2, 0, -10)
		tip1:SetHyperlinkCompareItem(link, changed and 2 or 1)
		tip1:Show()
		tip1:SetBackdropColor(0.0, 0.0, 0.5)

		if item2 then
			tip2:SetOwner(tip, "ANCHOR_NONE")
			tip2:SetHyperlinkCompareItem(link, changed and 1 or 2)
			tip2:Show()
			tip2:ClearAllPoints()
			if tip2:GetWidth() > left or tip2:GetWidth() > (GetScreenWidth() - right) then
				tip2:SetPoint(anchor1, tip1, anchor2, 0, 0)
			else
				tip2:SetPoint(anchor2, tip, anchor1, 0, -10)
			end
			tip2:SetBackdropColor(0.0, 0.0, 0.5)
		end
	end
end


-- options
tdCompareDB = {
	chatshowtip = true,
	chatshowequip = true,
	chatshift = true,
	actionshowequip = false,--nil
	refshowequip = true,
	shift = nil,
	rating = true,
	itemlevel = false,--true
	itemid = false,--true
}

local option = CreateFrame("Frame", UIParent)
option:Hide()

local function OnClick(self)
	tdCompareDB[self.var] = self:GetChecked() and true or nil
end

local function OnShow(self)
	self:SetChecked(tdCompareDB[self.var])
end

function option:CreateCheck(var, num, move, addon)
	local frame = CreateFrame("CheckButton", nil, self, "OptionsCheckButtonTemplate")
	local label = frame:CreateFontString(nil,"ARTWORK","GameFontNormal")
	label:SetPoint("LEFT",frame,"RIGHT")
	frame:SetFontString(label)
	frame:SetScript("OnClick", OnClick)
	frame:SetScript("OnShow", OnShow)
	frame.var = var
	frame:SetPoint("TOPLEFT", 10 + (move or 0) * 30, - 10 - num * 30)
	if addon and IsAddOnLoaded(addon) then
		frame:Disable()
		tdCompareDB[var] = nil
		label:SetFormattedText("%s%s", L[var], format(L.disable, addon))
	else
		label:SetText(L[var])
	end
	OnShow(frame)

	return num + 1
end

local norating
function option:Load()
	local num = 0
	num = self:CreateCheck("chatshowtip",     num, 0)
	num = self:CreateCheck("chatshowequip",   num, 1)
	num = self:CreateCheck("chatshift",       num, 2)
	num = self:CreateCheck("actionshowequip", num, 0)
	num = self:CreateCheck("refshowequip",    num, 0)
	num = self:CreateCheck("shift",           num, 0) + 1
	num = self:CreateCheck("rating",          num, 0, "RatingBuster")
	num = self:CreateCheck("itemlevel",       num, 0, "RatingBuster")
	num = self:CreateCheck("itemid",          num, 0, "RatingBuster")
	option:SetScript("OnShow", nil)
end

function option:Event(event, ...)
	if event == "ADDON_LOADED" and arg1 == "Blizzard_GuildBankUI" then
		HookFrame(GuildBankMessageFrame, "OnHyperlinkEnter", OnHyperlinkEnter)
		HookFrame(GuildBankMessageFrame, "OnHyperlinkLeave", OnHyperlinkLeave)
		GuildBankMessageFrame.UpdateTooltip = UpdateTooltip
		self:UnregisterEvent(event)
	end
end

option:SetScript("OnShow", option.Load)
option:SetScript("OnEvent", option.Event)
option:RegisterEvent("ADDON_LOADED")

option.name = "tdCompare"
InterfaceOptions_AddCategory(option)
